//
//  UIFlowLightViewController.swift
//  iPhone
//
//  Created by Toj on 3/28/23.
//

import UIKit

// MARK: - 懒加载(属性), 变量, 初始化init
class UIFlowLightViewController: UIViewController {
    
    private lazy var heartbeatStartButton: UIButton = {
        let view = UIButton()
        view.backgroundColor = .random
        view.setTitle("HBView 开始", for: .normal)
        view.setTitleColor(.red, for: .normal)
        view.addTarget(self, action: #selector(didTapHeartbeatStart), for: .touchUpInside)
        return view
    }()
    
    private lazy var heartbeatStopButton: UIButton = {
        let view = UIButton()
        view.backgroundColor = .random
        view.setTitle("HBView 结束", for: .normal)
        view.addTarget(self, action: #selector(didTapHeartbeatStop), for: .touchUpInside)
        return view
    }()
    
    private lazy var flowLightStartButton: UIButton = {
        let view = UIButton()
        view.backgroundColor = .random
        view.setTitle("FLView 开始", for: .normal)
        view.addTarget(self, action: #selector(didTapFlowLightStart), for: .touchUpInside)
        return view
    }()

    private lazy var flowLightStopButton: UIButton = {
        let view = UIButton()
        view.backgroundColor = .random
        view.setTitle("FLView 结束", for: .normal)
        view.addTarget(self, action: #selector(didTapFlowLightStop), for: .touchUpInside)
        return view
    }()

    private lazy var startFlowLightButton: UIButton = {
        let view = UIButton()
        view.backgroundColor = .random
        view.setTitle("分类 FL 开始", for: .normal)
        view.addTarget(self, action: #selector(didTapStartFlowLight), for: .touchUpInside)
        return view
    }()

    private lazy var stopFlowLightButton: UIButton = {
        let view = UIButton()
        view.backgroundColor = .random
        view.setTitle("分类 FL 结束", for: .normal)
        view.addTarget(self, action: #selector(didTapStotFlowLight), for: .touchUpInside)
        return view
    }()
    
    private let gradientColors = ["F3D48B", "CDAC49"]
    private lazy var flowLabel: UIButton = {
        let view = UIButton()
        view.setTitleColor("#775E33".to_color, for: .normal)
        view.backgroundColor = "#F3D48B".to_color
        view.setTitle("PAY", for: .normal)
        
        let width = CGFloat.sr_width - 16 * 2
        let size = CGSize(width: width, height: 44)
        view.frame = CGRect(origin: .zero, size: size)
        view.cm_cornerRadius = size.height * 0.5
        view.gd_add(colors: gradientColors)
        
        return view
    }()
    
    private lazy var flowView: UIFlowLightView = {
        let view = UIFlowLightView()
        view.duration = 0.7
        view.repeatCount = 3
        view.timeInterval = 0.3
        
        let width = CGFloat.sr_width - 16 * 2
        let size = CGSize(width: width, height: 44)
        view.frame = CGRect(origin: .zero, size: size)
        view.cm_cornerRadius = size.height * 0.5
        view.gd_add(colors: gradientColors)
        
        return view
    }()
    
    private lazy var heartbeatView: UIHeartbeatView = {
        let view = UIHeartbeatView()
        view.duration = 0.7
        view.repeatCount = 3
        view.scope = 0.95
        
        let width = CGFloat.sr_width - 16 * 2
        let size = CGSize(width: width, height: 44)
        view.frame = CGRect(origin: .zero, size: size)
        view.cm_cornerRadius = size.height * 0.5
        view.gd_add(colors: gradientColors)
        
        return view
    }()
    
    private lazy var textField: UITextField = {
        let view = UITextField()
        view.backgroundColor = UIColor.gray
        view.autocorrectionType = .no
        view.autocapitalizationType = .none
        return view
    }()
 
    private lazy var textField2: UITextField = {
        let view = UITextField()
        view.backgroundColor = UIColor.gray
        view.autocorrectionType = .no
        view.autocapitalizationType = .none
        return view
    }()
    
    var indexx: UInt32 = 0
}

// MARK: - 生命周期
extension UIFlowLightViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeInitialize()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
        
        interstitialAdRequest()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    }
}

// MARK: - 数据请求
private extension UIFlowLightViewController {
   
    private func interstitialAdRequest() {
        STAdManager.manager.requestInterstitialAd { [weak self] response in
            self?.interstitialAdResponse(response: response)
        }
    }
    
    private func interstitialAdResponse(response: STAdmobModel?) {
        guard let admobModel = response else { return }
        let isTrigger = STAdmobManager.manager.isTriggerInterstitial(admobModel: admobModel)
        guard isTrigger else { return }
        
        STAdmobManager.manager.loadAd(admobModel: admobModel) { isSuccess in
            if !isSuccess { return }
            STAdmobManager.manager.presentAd(admobModel: admobModel)
        }
    }
}

// MARK: - 点击事件
private extension UIFlowLightViewController {
    
    func complation(_ block: ((UInt32)->Void)) {
        print("-->toj: complation:1")
        if indexx > 0 {
            return
        }
        print("-->toj: complation2")
        let arc = arc4random_uniform(5)
        block(arc)
        indexx = arc
    }
    
    func request(_ block: ((UInt32)->Void)) {
        print("-->toj: indx")
        indexx = 0
        complation { indx in
            print("-->toj: indx:\(indx)")
            block(indx)
        }
    }
    
    @objc func didTapHeartbeatStart() {
        for _ in 0..<3 {
            request { inx in
                print("-->toj: result:\(inx)")
            }
        }
        
        
        
//        flowLabel.fl_startAnimation(color: "#CDAC49".to_color, duration: 6, repeatCount: 1)
        
//        flowView.startFlowLightAnimation()
//        flowView.startHeartbeatAnimation()
    }
    
    @objc func didTapHeartbeatStop() {
        
        flowLabel.fl_stopAnimation()
        flowView.stopFlowLightAnimation()
    }
     
    @objc func didTapFlowLightStart() {
        flowLabel.fl_startAnimation(color: .red, duration: 3)
    }
    
    @objc func didTapFlowLightStop() {
        let dur = textField.text ?? ""
        let duration = dur.to_double
        let color = textField2.text ?? ""
        
        flowLabel.fl_startAnimation(color: color.to_color, duration: duration, timeInterval: 0.3, repeatCount: 3, animationDelegate: self)
        flowView.startAnimation()
    }
    
    @objc func didTapStartFlowLight() {
        
    }
    
    @objc func didTapStotFlowLight() {
        
    }
}

// MARK: - Private
private extension UIFlowLightViewController {
    
}

// MARK: - 代理事件
extension UIFlowLightViewController: CAAnimationDelegate {
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        flowLabel.fl_stopAnimation()
    }
}

// MARK: - 初始化设置
private extension UIFlowLightViewController {
    
    func setupMakeAddSubviews() {
        let subViews = [
            textField,
            textField2,
            
            heartbeatStartButton,
            heartbeatStopButton,
            flowLightStartButton,
            flowLightStopButton,
            startFlowLightButton,
            stopFlowLightButton,
            
            flowLabel,
            flowView
        ]
        
        subViews.forEach { subView in
            view.addSubview(subView)
        }
    }
    
    func setupMakeLayoutSubviews() {
        var size = CGSize.zero
        
        textField.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(16)
            make.top.equalTo(textField2.snp.bottom).offset(20)
            make.height.equalTo(44)
        }
        textField2.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(16)
            make.top.equalTo(250)
            make.height.equalTo(44)
        }
        
        size = flowLabel.frame.size
        flowLabel.snp.remakeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(textField.snp.bottom).offset(20)
            make.size.equalTo(size)
        }
        
        size = flowView.frame.size
        flowView.snp.remakeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(flowLabel.snp.bottom).offset(20)
            make.size.equalTo(size)
        }
    }
    
    func setupMakeInitialize() {
        view.backgroundColor = .white
        
        let max = UInt32.max - 100000
        let random = arc4random_uniform(max) + 100000
        textField.text = String(random)
        textField.text = "0.7"
        textField2.text = "#FFFFFF"
        
        let navY = navigationController?.nav_barFullHeight ?? 0
        
        let column = 2
        let width = 150.to_cgfloat
        let height = 44.to_cgfloat
        let radius = height * 0.5
        
        let marginY = 30.to_cgfloat
        let marginX = (CGFloat.sr_width - CGFloat(column) * width) / (CGFloat(column + 1))
        var x = CGFloat.zero
        var y = CGFloat.zero
        
        let subViews = [
            heartbeatStartButton,
            heartbeatStopButton,
            flowLightStartButton,
            flowLightStopButton,
            startFlowLightButton,
            stopFlowLightButton
        ]
        var indexI = 0
        subViews.forEach { subview in
            let clm = indexI % column
            let row = indexI / column
            x = marginX + CGFloat(clm) * (marginX + width)
            y = navY + CGFloat(row) * (marginY + height)
            subview.frame  = CGRect(x: x, y: y, width: width, height: height)
            
            subview.cm_shadow(color: UIColor.black.tt_alpha(0.4), opacity: 1, radius: 5, offset: CGSize(width: 0, height: 3), corner: radius)
            
            indexI += 1
        }
    }
}
